<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * This is the model class for table "user".
 *
 * @property string $id 自增id
 * @property string $email 邮件
 * @property string $password 密码
 * @property string $created 创建时间
 */
class UserForm extends Model
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }

    public $password;
    public $email;
    public $password_res;
    public $code;
    public $test;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['password', 'email','code','password_res','test'], 'required'],
            ['email', 'email'],
            [['email'], 'string', 'max' => 50],
            [['password','password_res'], 'string', 'max' => 32],
            ['password_res','compare','compareAttribute'=>'password'],
            [['code'],'integer','min' => 100000,'max' => 999999],
            [['code'],'validateCode'],
        ];
    }


    public function getMD5Password(){

        return md5($this->password);
    }

    public function validateCode($attr, $params)
    {
        $code = \Yii::$app->cache->get($this->email);
        if($code == $this->code){

            return;
        }else{
            $this->addError('code', 'code error.');
        }

    }

}
